package com.niit.cms.controller;

import com.niit.cms.entity.Slideshow;
import com.niit.cms.entity.Userinfo;
import com.niit.cms.service.SlideshowService;
import com.niit.cms.service.UserinfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: cao_jiaqi
 * @CreateTime： 2022/6/22 14:02
 */
@Controller
public class LoginController {
    @Resource
    private SlideshowService slideshowService;
    @Resource
    private UserinfoService userinfoService;

    /**
     * @Description: 登录跳转请求
     * @Author: cao_jiaqi
     * @CreateTime: 2022/6/21 14:46
     * @return: java.lang.String
     **/
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    /**
     * @Description: 实现用户点击登录按钮，处理登录逻辑，验证账号，密码
     * @Author: cao_jiaqi
     * @CreateTime: 2022/6/21 15:15
     * @param: userCode
     * @param: userPassword
     * @return: java.lang.String
     **/
    @PostMapping("/login")
    public String toLogin(HttpSession session, String username, String password){
        System.out.println("username = " + username +" password = " + password);
        //验证账号密码是否正确，从数据库中验证
        Userinfo user = userinfoService.queryByUsernamePassword(username, password);
        if( null == user){
            return "login";
        }
//        if("admin".equals(userCode) && "123456".equals(userPassword)){
//            session.setAttribute("user", userCode);
//            //return "index1";
//            return "redirect:/";
//        }
        session.setAttribute("user", user);
        return "redirect:/index";
    }
    /**
     * @Description: 实现退出登录
     * @Author: cao_jiaqi
     * @CreateTime: 2022/6/22 8:46
     * @param: session
     * @return: java.lang.String
     **/
    //将lougout请求地址映射到 退出登录控制器方法
    @GetMapping("/logout")
    public String logout(HttpSession session){
        //将session中的登录用户信息删除，实现退出功能
        session.removeAttribute("user");
        //将页面重定向到登录页面
        return  "redirect:/login";
    }

    @GetMapping("/register")
    public String reg(Model model, HttpSession session){
        Userinfo userinfo = new Userinfo();
        model.addAttribute("user", userinfo);
        return  "reg";
    }
    /**
     * @Description: 注册功能，向数据库插入一条用户数据
     * @Author: cao_jiaqi
     * @CreateTime: 2022/6/23 16:22
     * @param: userinfo
     * @return: java.lang.String
     **/
    @PostMapping("/register")
    public String toReg(Model model, @Validated Userinfo userinfo, BindingResult result){
        //判断校验对象中是否有错误信息
        if(result.hasErrors()){
            Map<String, String> errMap = new HashMap<>();
            for(FieldError error : result.getFieldErrors()){
                errMap.put(error.getField(), error.getDefaultMessage());
            }
            model.addAttribute("errors", errMap);
            model.addAttribute("user", userinfo);
            return "reg";
        }
        System.out.println("注册用户"+userinfo);
        Userinfo u = userinfoService.insert(userinfo);
        if(u != null){
            return "redirect:/login";
        }
        return "reg";
    }

}
